#!/bin/bash
#功能：导入国六车辆发动机异常阈值数据

#初始化环境变量
source $(cd "$(dirname "$0")"; pwd)/../../initEnv.sh
table_name=engine_threshold
target_table_name=ods_cn6_engine_threshold
echo "导入${target_table_name}开始..."

sqoop-import \
  -D mapred.job.queue.name=hive \
  -D sqoop.parquet.logical_types.decimal.enable=true \
  -D parquetjob.configurator.implementation=hadoop \
  -D sqoop.parquet.decimal_padding.enable=true \
  -D sqoop.parquet.logical_types.decimal.default.precision=32 \
  -D sqoop.parquet.logical_types.decimal.default.scale=2 \
  --connect $CN6_CONNECT \
  --username $CN6_USERNAME \
  --password $CN6_PASSWORD \
  --query 'select
    engine_type,
    cast(IF(speed_max is null, 9223372036854775807, speed_max) as decimal(32, 2)) as speed_max,
    cast(IF(speed_min is null, -9223372036854775808, speed_min) as decimal(32, 2)) as speed_min,
    cast(IF(atmospheric_pressure_max is null, 9223372036854775807, atmospheric_pressure_max) as decimal(32, 2)) as barometric_pressure_max,
    cast(IF(atmospheric_pressure_min is null, -9223372036854775808, atmospheric_pressure_min) as decimal(32, 2)) as barometric_pressure_min,
    cast(IF(engine_out_put_torque_max is null, 9223372036854775807, engine_out_put_torque_max) as decimal(32, 2)) as output_torque_max,
    cast(IF(engine_out_put_torque_min is null, -9223372036854775808, engine_out_put_torque_min) as decimal(32, 2)) as output_torque_min,
    cast(IF(friction_torque_max is null, 9223372036854775807, friction_torque_max) as decimal(32, 2)) as friction_torque_max,
    cast(IF(friction_torque_min is null, -9223372036854775808, friction_torque_min) as decimal(32, 2)) as friction_torque_min,
    cast(IF(engine_speed_max is null, 9223372036854775807, engine_speed_max) as decimal(32, 2)) as rotation_max,
    cast(IF(engine_speed_min is null, -9223372036854775808, engine_speed_min) as decimal(32, 2)) as rotation_min,
    cast(IF(fuel_consum_ption_rate_max is null, 9223372036854775807, fuel_consum_ption_rate_max) as decimal(32, 2)) as fuel_consumption_max,
    cast(IF(fuel_consum_ption_rate_min is null, -9223372036854775808, fuel_consum_ption_rate_min) as decimal(32, 2)) as fuel_consumption_min,
    cast(IF(scr_upstream_noxsensor_out_put_max is null, 9223372036854775807, scr_upstream_noxsensor_out_put_max) as decimal(32, 2)) as scr_up_nox_max,
    cast(IF(scr_upstream_noxsensor_out_put_min is null, -9223372036854775808, scr_upstream_noxsensor_out_put_min) as decimal(32, 2)) as scr_up_nox_min,
    cast(IF(scr_down_stream_noxsensor_out_put_max is null, 9223372036854775807, scr_down_stream_noxsensor_out_put_max) as decimal(32, 2)) as scr_down_nox_max,
    cast(IF(scr_down_stream_noxsensor_out_put_min is null, -9223372036854775808, scr_down_stream_noxsensor_out_put_min) as decimal(32, 2)) as scr_down_nox_min,
    cast(IF(reagent_allowance_max is null, 9223372036854775807, reagent_allowance_max) as decimal(32, 2)) as reagent_allowance_max,
    cast(IF(reagent_allowance_min is null, -9223372036854775808, reagent_allowance_min) as decimal(32, 2)) as reagent_allowance_min,
    cast(IF(air_inflow_max is null, 9223372036854775807, air_inflow_max) as decimal(32, 2)) as air_inflow_max,
    cast(IF(air_inflow_min is null, -9223372036854775808, air_inflow_min) as decimal(32, 2)) as air_inflow_min,
    cast(IF(scr_inlet_temperature_max is null, 9223372036854775807, scr_inlet_temperature_max) as decimal(32, 2)) as scr_in_max,
    cast(IF(scr_inlet_temperature_min is null, -9223372036854775808, scr_inlet_temperature_min) as decimal(32, 2)) as scr_in_min,
    cast(IF(scr_outlet_temperature_max is null, 9223372036854775807, scr_outlet_temperature_max) as decimal(32, 2)) as scr_out_max,
    cast(IF(scr_outlet_temperature_min is null, -9223372036854775808, scr_outlet_temperature_min) as decimal(32, 2)) as scr_out_min,
    cast(IF(dpf_dropout_voltage_max is null, 9223372036854775807, dpf_dropout_voltage_max) as decimal(32, 2)) as dropout_voltage_max,
    cast(IF(dpf_dropout_voltage_min is null, -9223372036854775808, dpf_dropout_voltage_min) as decimal(32, 2)) as dropout_voltage_min,
    cast(IF(engine_coolant_temperature_max is null, 9223372036854775807, engine_coolant_temperature_max) as decimal(32, 2)) as coolant_temperature_max,
    cast(IF(engine_coolant_temperature_min is null, -9223372036854775808, engine_coolant_temperature_min) as decimal(32, 2)) as coolant_temperature_min,
    cast(IF(tank_level_max is null, 9223372036854775807, tank_level_max) as decimal(32, 2)) as tank_level_max,
    cast(IF(tank_level_min is null, -9223372036854775808, tank_level_min) as decimal(32, 2)) as tank_level_min,
    cast(IF(ternary_up_output_max is null, 9223372036854775807, ternary_up_output_max) as decimal(32, 2)) as ternary_up_output_max,
    cast(IF(ternary_up_output_min is null, -9223372036854775808, ternary_up_output_min) as decimal(32, 2)) as ternary_up_output_min,
    cast(IF(ternary_down_output_max is null, 9223372036854775807, ternary_down_output_max) as decimal(32, 2)) as ternary_down_output_max,
    cast(IF(ternary_down_output_min is null, -9223372036854775808, ternary_down_output_min) as decimal(32, 2)) as ternary_down_output_min,
    cast(IF(ternary_temperature_output_max is null, 9223372036854775807, ternary_temperature_output_max) as decimal(32, 2)) as ternary_temperature_output_max,
    cast(IF(ternary_temperature_output_min is null, -9223372036854775808, ternary_temperature_output_min) as decimal(32, 2)) as ternary_temperature_output_min
  from cn6_engine_flow_threshold_set where 1=1 or $CONDITIONS' \
  --fields-terminated-by "\0001" \
  --delete-target-dir \
  --target-dir /user/root/mysql_tables/${table_name} \
  --hive-import \
  --hive-overwrite \
  --hive-table ${target_table_name} \
  --hive-database ${HIVE_DB} \
  --hive-drop-import-delims \
  --num-mappers 1 \
  --as-parquetfile;


if [ $? -ne 0 ] ; then
  echo "导入ods_cn6_engine_threshold失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入ods_cn6_engine_threshold成功，耗时$((`date +%s`-START_TIME))秒..."
fi
